package tptecnicas.printers;

import tptecnicas.logging.MessageInfo;

/**
 * Clase encargada de la resolucion de cada nivel de log.
 * Compara y filtra los mensajes con nivel superior al configurado.
 */
public class Level {
		
	private final int TRACE = 6, DEBUG = 5, INFO = 4, WARNING = 3, ERROR = 2, FATAL = 1, OFF = 0;
	private int instance;
	
	public Level(String logLevel) {
		this.instance = this.parseLevel(logLevel);
	}
	
	public String filterMessage(MessageInfo message) {
		if (!this.isMessageLevelAboveCurrentLevel(message)) {
			return message.getMessage();
		}
		return null;
	}

	private boolean isMessageLevelAboveCurrentLevel(MessageInfo message) {
		return this.parseLevel(message.getLevel()) > this.instance;
	}
	
	public int getType() {
		return this.instance;
	}

	public boolean hasLevelEnabled(String level) {
		return this.parseLevel(level) <= this.instance;
	}
	
	private int parseLevel(String logLevel) {	 
		switch(logLevel) {
		case "TRACE":
			return this.TRACE;
		case "INFO":
			return this.INFO;
		case "DEBUG":
			return this.DEBUG;
		case "WARNING":
			return this.WARNING;
		case "ERROR":
			return this.ERROR;
		case "FATAL":
			return this.FATAL;
		default:
			return this.OFF;
		}
	}
}
